//
//  BIZAlertView.m
//  biz
//
//  Created by zhangfuwei on 2017/3/20.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZAlertView.h"

@interface BIZAlertView ()<UIAlertViewDelegate>

/** 注释内容 */
@property (nonatomic, copy) BIZAlertViewCancelBlock cancelBlock;

/** 注释内容 */
@property (nonatomic, copy) BIZAlertViewConfirmBlock confirmBlock;

@end

@implementation BIZAlertView
+(id)sharedAlertViewTool {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)showAlertViewWithTitle:(NSString *)alertTitle message:(NSString *)msg cancelButtonTitle:(NSString *)cancelBtnTitle confirmButtonTitle:(NSString *)confirmBtnTitle cancelBlock:(BIZAlertViewCancelBlock)cancelBlock confirmBlock:(BIZAlertViewConfirmBlock)confirmBlock {
    
    _cancelBlock = cancelBlock;
    _confirmBlock = confirmBlock;
    
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0f) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                            message:msg
                                                           delegate:self
                                                  cancelButtonTitle:cancelBtnTitle?:@"OK"
                                                  otherButtonTitles:confirmBtnTitle?:nil,nil];
        
        [alertView show];
        
    }else {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        //  根据确定文案是否为空来 创建添加action
        if (cancelBtnTitle) {
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (self.cancelBlock != nil) {
                    
                    self.cancelBlock();
                }
            }];
            [alertController addAction:cancelAction];
        }

        //  根据确定文案是否为空来 创建添加action
        if (confirmBtnTitle) {
            
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmBtnTitle?:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (self.confirmBlock != nil) {
                    
                    self.confirmBlock();
                }
            }];
            
            [alertController addAction:confirmAction];
        }
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
        
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex) {
        if (self.confirmBlock) {
            self.confirmBlock();
        }
    }else {
        if (self.cancelBlock) {
            self.confirmBlock();
        }
    }
}

@end
